<script>
import $http from './utils/http/index.js'
import $common from './utils/function/common'
import { initWebSocket } from '@/utils/function/ws.js'; // 引入 WebSocket 初始化函数

export default {
  globalData: {
    $http: $http,
    $common: $common,
    now_version: '1.0',
    use_car_onShow: true
  },

  onLaunch: function () {
    //#ifdef APP-PLUS
    plus.screen.lockOrientation('portrait-primary'); // 强制竖屏
    //#endif
    this.preloadStartImage(); // 预加载启动页 GIF

    var user_id = uni.getStorageSync('user_id');
    if(user_id > 0){
      initWebSocket('wss://www.xidaola1314.com/wss'); // 初始化 WebSocket 连接
      // initWebSocket('ws://127.0.0.1:8282'); // 初始化 WebSocket 连接
    }
  },

  methods: {
    /**
     * 预加载启动页 GIF
     */
    preloadStartImage() {
      const cachedImg = uni.getStorageSync('start_img');
      const lastModifiedCache = uni.getStorageSync('start_img_last_modified'); // 读取上次缓存的时间戳
      const imgUrl = this.globalData.$http.config.baseURL + '/static/xcx/start.gif';

      // **发送 HEAD 请求获取 `Last-Modified`**
      uni.request({
        url: imgUrl,
        method: 'HEAD',
        success: (res) => {
          const lastModified = res.header['Last-Modified']; // 服务器返回的时间戳
          if (!lastModified || lastModifiedCache === lastModified) {
            console.log("图片未更新，使用缓存");
            return;
          }

          console.log("检测到新图片，开始下载...");
          uni.downloadFile({
            url: imgUrl,
            success: (res) => {
              if (res.statusCode === 200) {
                uni.setStorageSync('start_img', res.tempFilePath);
                uni.setStorageSync('start_img_last_modified', lastModified);
                console.log("新启动图下载完成并缓存！");
              }
            },
            fail: (err) => {
              console.error("GIF 下载失败", err);
            }
          });
        },
        fail: (err) => {
          console.error("HEAD 请求失败，无法检查启动图更新", err);
        }
      });
    }
  },
}
</script>

<style lang="scss">

.app-container {
  font-size: 28rpx;
}

.main-background {
  background: rgba(199, 169, 74, 0.2);
}

.background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  //opacity: 0.5;
  object-fit: cover; /* 保持比例填充 */
  z-index: -1; /* 确保背景图在最底层 */
}

/*每个页面公共css */

checkbox {
  transform: scale(1);
}

//设置圆角
checkbox.round .wx-checkbox-input,
checkbox.round .uni-checkbox-input {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
}

//设置背景色
checkbox.color[checked] .wx-checkbox-input,
checkbox.color.checked .uni-checkbox-input {
  background-color: $main-color !important;
  border-color: $main-color !important;
  color: #ffffff !important;
}

.bar-box {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;

  .back {
    position: absolute;
    left: 30rpx;
    top: 0;
    width: 34rpx;
    height: 34rpx;
  }

  .title {
    color: $main-color;
    font-size: 40rpx;
  }
}

::-webkit-scrollbar {
  display: none;
  width: 0 !important;
  height: 0 !important;
  -webkit-appearance: none;
  background: transparent;
  color: transparent;
}

// 登录弹框

#mask {
  position: fixed;
  left: 0;
  top: 0;
  background: rgba(0, 0, 0, 0.5);
  width: 100%;
  height: 100%;
  filter: alpha(opacity=100);
  opacity: 1;
  display: block;
  overflow: hidden;
  z-index: 99999;

  .mask-content {
    display: flex;
    position: fixed;
    padding: 20rpx 20rpx 240rpx 20rpx;
    width: calc(100% - 40rpx);
    left: 0;
    bottom: 0;
    z-index: 999999;
    flex-direction: column;
    background: #fff;
    height: 490rpx;
    border-radius: 28rpx 28rpx 0 0;

    .main-content {
      padding: 20rpx 40rpx;

      .title {
        font-size: 32rpx;
        text-align: center;
      }

      .avatar-btn {
        margin-top: 60rpx;
        background-color: white;

        .btnImg {
          width: 120rpx;
          height: 120rpx;
          border-radius: 60rpx;
        }
      }

      .avatar-btn::after {
        border: 0;
      }

      .row {
        display: flex;
        width: 100%;
        justify-content: space-between;
        align-items: center;
        padding: 10rpx 0;
        border-top: 1rpx solid #e5e5e5;
        border-bottom: 1rpx solid #e5e5e5;

        .left {
          width: 120rpx;
        }

        .mobile {
          padding: 20rpx;
        }

        .input {
          border: none;
          padding: 20rpx;
          border-radius: 20rpx;
          text-align: right;
          flex: 1;
        }

        .mobile-btn {
          color: #fff;
          background: #F30E0E;
          padding: 0 30rpx;
          font-size: 28rpx;
          margin-left: 20rpx;
          border: none;
        }
      }

      .btn-box {
        display: flex;
        justify-content: space-between;
        width: 100%;
        margin-top: 40rpx;

        .button {
          font-size: 32rpx;
          flex: 1;
          text-align: center;
          border-radius: 50rpx;
        }

        .cancel {
          background: #9d9b9b;
          color: #fff;
        }

        .save {
          background: #F30E0E;
          color: #fff;
          margin-left: 40rpx;
          border: none;
          outline: none;
        }
      }

    }

    .agree-box {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;

      .agree-btn {
        margin: 0;
        padding: 0;
        position: static;
        background-color: transparent;
        height: 104rpx;
        width: 60rpx;
      }

      .agree-btn::after {
        border: 0;
        content: none;
      }

      .box {
        font-size: 32rpx;
        color: #666;

        .text {
          color: #F30E0E;
        }
      }
    }
  }
}

.share-btn {
  position: fixed;
  border: none;
  padding: 0;
  margin: 0;
  right: 20rpx;
  bottom: 170rpx;
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;

  .share-view{
    position: fixed;
    background: $main-color;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    right: 20rpx;
    bottom: 170rpx;
    width: 130rpx;
    height: 130rpx;
    border-radius: 50%;

    .share-icon {
      width: 40rpx;
      height: 44rpx;
      margin-bottom: 10rpx;
    }
    text{
      color: #fff;
      font-size: 32rpx;
      line-height: 32rpx;

    }
  }
}

</style>
